/*
 * ReportController.java
 *
 * Created on January 26, 2009, 3:37 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.vacuela.publishmx.web.controllers;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.vacuela.publishmx.domains.Message;
import org.vacuela.publishmx.domains.OutputMessage;
import org.vacuela.publishmx.services.ReportService;
import org.vacuela.publishmx.util.PublishUtil;
import org.vacuela.publishmx.context.ServiceContext;
import org.vacuela.publishmx.context.ServiceContextImpl;
import org.vacuela.publishmx.domains.ReportCriteria;

/**
 *
 * @author nestor.carrasco
 */
public class ReportController extends MultiActionController{
    
    ReportService reportSvc;
    /** Creates a new instance of ReportController */
    public ReportController() {
    }
    
    public ModelAndView searchReportView(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
        ModelAndView mav = new ModelAndView("report/reportManage");
        String msgType = PublishUtil.checkNull(request.getParameter("typeSms"),"");
        String fromDate = PublishUtil.checkNull(request.getParameter("fromDate"),"");
        String toDate = PublishUtil.checkNull(request.getParameter("toDate"),"");
        Map<String, List<Message>> msgMap = null;
        if (!"".equals(msgType ) && !"".equals(fromDate ) &&  !"".equals(fromDate ) ){
            ReportCriteria criteria = new ReportCriteria(msgType,fromDate,toDate);
            ServiceContext context = new ServiceContextImpl();
            context.setAttribute("reportCriteria", criteria);
            reportSvc.getReportMessages( context);
            msgMap = ( Map<String, List<Message>>) context.getAttribute("msgMap");
            mav.addObject("msgOutAgenda", msgMap.get("msgOutAgenda"));
            mav.addObject("msgOutInd", msgMap.get("msgOutInd"));
            mav.addObject("msgIn", msgMap.get("msgIn"));
        }
        mav.addObject("typeSms",msgType);
        mav.addObject("fromDate",fromDate);
        mav.addObject("toDate",toDate);
        return mav;
    }
    
    
    public ModelAndView showListPhoneNumbers(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
        ModelAndView mav = new ModelAndView("report/reportManage");
        String msgId = PublishUtil.checkNull(request.getParameter("msgId"),"");
        String typeOut = PublishUtil.checkNull(request.getParameter("typeOut"),"");
        if (!"".equals(msgId) && !"".equals(typeOut)){
            ServiceContext context = new ServiceContextImpl();
            context.setAttribute("typeOut",typeOut);
            context.setAttribute("msgId",Integer.valueOf(msgId));
            reportSvc.getOutputPhoneNumbers(context);
            OutputMessage outMsg = (OutputMessage)context.getAttribute("outMsg");
            mav.addObject("outMsg",outMsg);
        }
        return mav;
    }
    
    
    public void setReportService(ReportService service){
        reportSvc = service;
    }
}
